String.prototype.format = function(args) { 
    if (arguments.length>0) { 
        var result = this; 
        if (arguments.length == 1 && typeof (args) == "object") { 
            for (var key in args) { 
                var reg=new RegExp ("({"+key+"})","g"); 
                result = result.replace(reg, args[key]); 
            } 
        } 
        else { 
            for (var i = 0; i < arguments.length; i++) { 
                if(arguments[i]==undefined) { 
                    return ""; 
                } 
                else { 
                    var reg=new RegExp ("({["+i+"]})","g"); 
                    result = result.replace(reg, arguments[i]); 
                } 
            } 
        } 
        return result; 
    } 
    else { 
        return this; 
    } 
};
 
cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        accountPanel:cc.Node,
        account:cc.EditBox,
        password:cc.EditBox,
    },

    // use this for initialization
    onLoad: function () {
        if(!cc.sys.isNative && cc.sys.isMobile){
            var cvs = this.node.getComponent(cc.Canvas);
            cvs.fitHeight = true;
            cvs.fitWidth = true;
        }
        
        if(!cc.vv){
            cc.director.loadScene("loading");
            return;
        }
        
        cc.vv.http.url = cc.vv.http.master_url;
        cc.vv.net.addHandler('push_need_create_role',function(){
            cc.director.loadScene("createrole");
        });
        
        cc.vv.audioMgr.playBGM("bgMain");
    },
    start:function(){
        var account =  cc.sys.localStorage.getItem("wx_account");
        var sign = cc.sys.localStorage.getItem("wx_sign");
        if(account != null && sign != null){
            var ret = {
                errcode:0,
                account:account,
                sign:sign
            }
            cc.vv.userMgr.onAuth(ret);
        }  
    },
    // 用户点击账号密码登录
    onBtnQuickStartClicked:function(){
        cc.find("Canvas/btn_yk").active = false;
        cc.find("Canvas/z_weixindenglu").active = false;
        this.accountPanel.active = true;
        // cc.vv.userMgr.accountLogin();
    },
    
    onBtnWeichatClicked:function(){
        var self = this;
        cc.vv.anysdkMgr.login();
    },
    onBtnLiginAndRegister:function(e,data){
        if (this.account.string == '' || this.password.string == ''){
            cc.vv.alert.show("温馨提示",'校验不通过！');
            return;
        }
        if (data == 'login'){//执行登录
            cc.vv.userMgr.accountLogin({
                account : this.account.string,
                password : this.password.string
            });
        }else{//执行注册
            cc.vv.userMgr.userRegister({
                account : this.account.string,
                password : this.password.string
            });
            
        }
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
